package com.media.selector.media.helper

import android.content.Intent
import androidx.annotation.NonNull
import androidx.annotation.Nullable
import androidx.fragment.app.FragmentActivity
import com.media.selector.MediaSelector
import com.media.selector.media.MediaActivity
import com.media.selector.media.filter.Filter
import com.media.selector.media.model.SelectionSpec
import com.media.selector.media.model.entity.MimeType

/**
 * date        ：2020/5/20
 * author      ：蒙景博
 * description ：
 */
class SelectionCreator constructor(@NonNull mimeTypes: Set<MimeType>){

    private val mSelectionSpec by lazy { SelectionSpec.clean() }

    init {
        mSelectionSpec.mimeTypeSet = mimeTypes
    }

    /**
     * 设置数据显示模式
     *
     * @param enable true:视频和图片一起显示，false:只显示一种，具体显示取决于 mimeTypes
     */
    fun setMediaTypeExclusive(enable: Boolean): SelectionCreator {
        mSelectionSpec.mediaTypeExclusive = enable
        return this
    }

    /**
     * 设置选择媒体数据模式
     * @param enable true:视频和图片可同时选择，false:只能选择图片
     */
    fun showSingleMediaType(enable: Boolean): SelectionCreator {
        mSelectionSpec.showSingleMimeType = enable
        return this
    }

    /**
     * 设置最大可选数量
     */
    fun maxSelectable(max: Int): SelectionCreator {
        if (max < 1) {
            throw IllegalArgumentException("最大可选数量必须大于或等于1!")
        } else if (mSelectionSpec.maxImageSelectable > 0 || mSelectionSpec.maxVideoSelectable > 0){
            throw IllegalArgumentException("已经设置了 maxImageSelectable 和 maxVideoSelectable")
        }
        mSelectionSpec.maxSelectable = max
        return this
    }

    /**
     * 设置视频和图片最大选择数量
     */
    fun maxSelectablePerMediaType(maxImageSelectable: Int, maxVideoSelectable: Int): SelectionCreator {
        if (maxImageSelectable < 1 || maxVideoSelectable < 1) throw IllegalArgumentException(("max selectable must be greater than or equal to one"))
        mSelectionSpec.maxSelectable = -1
        mSelectionSpec.maxImageSelectable = maxImageSelectable
        mSelectionSpec.maxVideoSelectable = maxVideoSelectable
        return this
    }

    /**
     * Add filter to filter each selecting item.
     *
     * @param filter {@link Filter}
     * @return {@link SelectionCreator} for fluent API.
     */
    fun addFilter(@NonNull filter: Filter): SelectionCreator {
        mSelectionSpec.filters.add(filter)
        return this;
    }

    /**
     * 是否启用相机
     */
    fun capture(enable: Boolean): SelectionCreator {
        mSelectionSpec.capture = enable
        return this
    }

    /**
     * 设置预览
     * @param enable
     */
    fun showPreview(enable: Boolean): SelectionCreator {
        mSelectionSpec.showPreview = enable
        return this
    }

    /**
     * Start to select media and wait for result.
     */
    fun start(@Nullable activity: FragmentActivity?) {
        activity?.let {
            it.startActivityForResult(Intent(it, MediaActivity::class.java), MediaSelector.MEDIA_SELECT_REQUEST_CODE)
        }
    }
}